/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.wizards.core.actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.Lookup;
import pt.iasd.youthshelter.datacore.controller.exceptions.NonexistentEntityException;
import pt.iasd.youthshelter.datacore.controller.exceptions.PreexistingEntityException;
import pt.iasd.youthshelter.datamodel.ActivityParticipation;
import pt.iasd.youthshelter.datamodel.service.ActivityParticipationController;
import pt.iasd.youthshelter.wizards.core.events.activity.ActivityParticipationTablePanel;

@ActionID(
        category = "Management",
        id = "pt.iasd.youthshelter.wizards.core.actions.ShowActivityPanelAction")
@ActionRegistration(
        iconBase = "pt/iasd/youthshelter/wizards/core/actions/icon.png",
        displayName = "Assign Activity")
@ActionReferences({
    @ActionReference(path = "Menu/Management", position = 400),
    @ActionReference(path = "Toolbars/Management", position = 400)
})
public final class ShowActivityParticipationPanelAction implements ActionListener {
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/wizards/core/Bundle");
    /* Logging object. */
    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Override
    public void actionPerformed(ActionEvent e) {
        ProgressHandle pHandle = ProgressHandleFactory.createHandle(
                    bundle.getString("ProgressBarWizard"));
        pHandle.start();
        final ActivityParticipationTablePanel activityParticipationPanel = new ActivityParticipationTablePanel();
        DialogDescriptor dialog = new DialogDescriptor(activityParticipationPanel, bundle.getString("ActivityParticipation.Panel"), true, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == DialogDescriptor.OK_OPTION) {
                    ActivityParticipationController controller = Lookup.getDefault().lookup(ActivityParticipationController.class);
                    for(ActivityParticipation activPart : activityParticipationPanel.getAddedActivityParticipations()) {
                        try {
                            controller.create(activPart);
                        } catch (PreexistingEntityException ex) {
                            logger.log(Level.WARNING, String.format(
                                    "%s participation in activity %s already exists.",
                                    activPart.getPathfinder().getName(),
                                    activPart.getActivity().getName()), ex);
                        } catch (Exception ex) {
                            logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
                            NotifyDescriptor dialog = new NotifyDescriptor.Message(
                                String.format(bundle.getString("ActivityParticipation.Save.Error")
                                            .replace("\n", System.getProperty("line.separator")),
                                        activPart.getPathfinder().getName(),
                                        activPart.getActivity().getName(),
                                        ex.getLocalizedMessage()),
                                NotifyDescriptor.ERROR_MESSAGE);
                            DialogDisplayer.getDefault().notify(dialog);
                        }
                    }
                    for(ActivityParticipation activPart : activityParticipationPanel.getRemovedActivityParticipations()) {
                        try {
                            controller.destroy(activPart.getActivityParticipationPK());
                        } catch (NonexistentEntityException ex) {
                            logger.log(Level.WARNING, String.format(
                                    "%s participation in activity %s does not exist.",
                                    activPart.getPathfinder().getName(),
                                    activPart.getActivity().getName()), ex);
                        }
                    }
                    for(ActivityParticipation activPart : activityParticipationPanel.getEditedActivityParticipations()) {
                        try {
                            controller.edit(activPart);
                        } catch (NonexistentEntityException ex) {
                            try {
                                controller.create(activPart);
                            } catch (Exception ex1) {
                                logger.log(Level.SEVERE, String.format(
                                        bundle.getString("ActivityParticipation.Edit.SaveError")
                                            .replace("\n", System.getProperty("line.separator")),
                                        activPart.getPathfinder().getName(),
                                        activPart.getActivity().getName(),
                                        ex.getLocalizedMessage(),
                                        ex1.getLocalizedMessage()), ex1);
                            }
                        } catch (Exception ex) {
                            logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
                            NotifyDescriptor dialog = new NotifyDescriptor.Message(
                                String.format(bundle.getString("ActivityParticipation.Edit.Error")
                                        .replace("\n", System.getProperty("line.separator")),
                                    activPart.getPathfinder().getName(),
                                    activPart.getActivity().getName(),
                                    ex.getLocalizedMessage()),
                                    NotifyDescriptor.ERROR_MESSAGE);
                            DialogDisplayer.getDefault().notify(dialog);
                        }
                    }
                }
            }
        });
        pHandle.finish();
        DialogDisplayer.getDefault().notify(dialog);
    }
}
